
importClass(android.content.ContextWrapper);
importClass(android.content.IntentFilter);
importClass(android.content.BroadcastReceiver);


importClass(android.app.Activity);
importClass(android.content.ComponentName);
importClass(android.content.ServiceConnection);
importClass(android.os.Bundle);
importClass(android.os.IBinder);
importClass(android.os.Message);
importClass(android.os.Messenger);
importClass(android.os.RemoteException);
importClass(android.view.View);

// /**
//     * 与服务端交互的Messenger
//     */
// var mService = null;

// /** Flag indicating whether we have called bind on the service. */
// var mBound;
// /**
//      * 实现与服务端链接的对象
//      */
// var mConnection = new ServiceConnection({
//     "onServiceConnected": function onServiceConnected(className, service) {
//         /**
//          * 通过服务端传递的IBinder对象,创建相应的Messenger
//          * 通过该Messenger对象与服务端进行交互
//          */
//         mService = new Messenger(service);
//         mBound = true;
//     },
//     "onServiceDisconnected": function onServiceDisconnected(className) {
//         // This is called when the connection with the service has been
//         // unexpectedly disconnected -- that is, its process crashed.
//         mService = null;
//         mBound = false;
//     }
// });

// function sayHello(v) {
//     if (!mBound) return;
//     // 创建与服务交互的消息实体Message
//     var msg = Message.obtain(null, 1, 0, 0);
//     try {
//         //发送消息
//         mService.send(msg);
//     } catch (error) {
//         log(e)
//     }
// }
// log(mService, mBound)

// //当前Activity绑定服务端
// var msgIntent = app.intent("zsl.ocr.receiver.img");
// msgIntent.setClassName("com.baidu.paddle.lite.demo.ocr", "com.baidu.paddle.lite.demo.ocr.MyService.class");
// context.bindService(msgIntent, mConnection,
//     context.BIND_AUTO_CREATE);

//     sayHello();
// sleep(60000)

//  context.unbindService(mConnection);
// mBound = false;



//接收结果
var filter = new IntentFilter();
var receiverAction = "zsl.ocr.receiver.word"
filter.addAction(receiverAction);
var cont = new ContextWrapper(context);
var receiver = new BroadcastReceiver({
    onReceive: function (context, intent) {
        log(intent.getStringExtra("data"));
        //取消广播
        context.unregisterReceiver(receiver)
    }
});
//注册广播
cont.registerReceiver(receiver, filter);

prepareThings();
var sendAction = "zsl.ocr.receiver.img"
//发送数据
var path = "/storage/emulated/0/DCIM/Camera/1/1.png";
var abPath = files.path(path)
captureScreen(path)
// var img0 = captureScreen();
// var a = images.toBytes(img0);
// log(a.length)
app.sendBroadcast(
  {
      action: sendAction,
      extras: {
          data: abPath,
          type: 'filePath'
      }
  }
);

sleep(10000)


function prepareThings() {
    //请求截图
    threads.start(function () {
        text("立即开始").findOne(3000).click();
    });
    if (!requestScreenCapture()) {
        toast("请求截图失败");
        exit();
    }
    sleep(1000);
}


//获取截图
function getCaptureImg() {
    try {
        var img0 = captureScreen();
        sleep(100);
        if (img0 == null || typeof (img0) == "undifined") {
            myFunc.log("截图失败,脚本退出");
            exit();
        } else {
            return img0;
        }
    } catch (error) {
        prepareThings();
        return getCaptureImg();
    }

}
